package com.systemmanger.controller;

import com.bean.WmsTCustomer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.systemmanger.service.KeHuGuanLiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

//客户管理的controller(KeHuGuanLi的service和mapper同理)
@Controller
@RequestMapping("guanli")
public class KeHuGuanLiController {
    @Autowired
    KeHuGuanLiService KHService;

    //发送请求：请求参数：currentPage=?&pageRows=?&查询条件分装成QueryForObject
    //前端将后台需要的分页参数信息传到后台，后台根据这些参数查询当前页信息（页码，条数，当前页信息，总页数），
    // 放入session域同时将刚刚前端也放入session；页面负责显示数据

    /**
     * 到该页面和上下页的方法
     *
     * @param httpSession
     * @param pageNum
     * @param pageSize
     * @param custName
     * @param custID
     * @param custIc
     * @return
     */
    @RequestMapping("YongHu.do")
    public String page(HttpSession httpSession,
                       @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                       @RequestParam(defaultValue = "15", value = "pageSize") Integer pageSize,
                       String custName, String custID, String custIc) {
        PageInfo<WmsTCustomer> pageInfo = new PageInfo<>();
        if (pageNum == null || pageNum < 0) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 15;
        }
        // WmsTCustomer wmsTCustomer=new WmsTCustomer();
        WmsTCustomer wmsC = new WmsTCustomer();
        wmsC.setCustName(custName);
        httpSession.setAttribute("custName", custName);
        wmsC.setCustIc(custIc);
        httpSession.setAttribute("custIc", custIc);
        wmsC.setCustID(custID);
        httpSession.setAttribute("custID", custID);
        //引入分页查询插件， pageNum是第几页，pageSize是每页显示多少条
        PageHelper.startPage(pageNum, pageSize);
        System.out.println(wmsC);
        List<WmsTCustomer> list = KHService.getCustomerMessage(wmsC);
        pageInfo = new PageInfo<WmsTCustomer>(list, pageSize);
        //   System.out.println(pageInfo);
        //System.out.println(pageInfo.getList());
        httpSession.setAttribute("pageInfo", pageInfo);
        return "guanli/ClientManger";
    }

    /**
     * 将查询到的值返回到客户信息列表
     *
     * @param custID
     * @return 单行的list
     */
    @RequestMapping("YongHu1.do")
    @ResponseBody
    public List<WmsTCustomer> page1(String custID) {
        WmsTCustomer wmsC = new WmsTCustomer();
        wmsC.setCustID(custID);
        // System.out.println(KHService.getCustomerMessage(wmsC));
        return KHService.getCustomerMessage(wmsC);
    }

    /**
     * 客户信息更新到数据库，返回给页面
     *
     * @param custName
     * @param custIc
     * @param custMobile
     * @param custID
     * @param custNote
     * @param state
     * @param session
     * @return
     */
    @RequestMapping("YongHu2.do")
    public String page2(String custName, String custIc, String custMobile, String custID,
                        String custNote, long state, HttpSession session) {
        if (state == 1) {
            KHService.deleteCustomerData(custID);
        } else {
            //日期更新
            LocalDate localDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String dateString = localDate.format(formatter);
            System.out.println(dateString);
            WmsTCustomer wmsC = new WmsTCustomer();
            wmsC.setCustID(custID);
            wmsC.setCustName(custName);
            wmsC.setCustIc(custIc);
            wmsC.setCustMobile(custMobile);
            wmsC.setCustNote(custNote);
            wmsC.setCustEditTime(dateString);
            System.out.println(wmsC);
            KHService.updateCustomerData(wmsC);

        }
        PageInfo<WmsTCustomer> pageInfo = new PageInfo<>();
        WmsTCustomer wmsC = new WmsTCustomer();
        wmsC.setCustID(custID);
        wmsC.setCustName(custName);
        wmsC.setCustIc(custIc);
        List<WmsTCustomer> list = KHService.getCustomerMessage(wmsC);
        pageInfo = new PageInfo<WmsTCustomer>(list, 1);
        session.setAttribute("pageInfo", pageInfo);
        return "guanli/ClientManger";
    }


}
